export function getCookie (name) {
  let arr
  let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  arr = document.cookie.match(reg)
  if (!arr) {
    return false
  }
  return unescape(arr[2])
}

export function setCookie (name, value, options) {
  let expires = ''
  let path = ''
  let domain = ''
  let secure = ''
  if (options) {
    if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {
      let exp
      if (typeof options.expires === 'number') {
        exp = new Date()
        exp.setTime(exp.getTime() + options.expires * 24 * 60 * 60 * 1000)
      } else {
        exp = options.expires
      }
      expires = ';expires=' + exp.toUTCString()
    }
    path = options.path ? '; path=' + options.path : ''
    domain = options.domain ? ';domain=' + options.domain : ''
    secure = options.secure ? ';secure' : ''
  }
  document.cookie = [name, '=', escape(value), expires, path, domain, secure].join('')
}

export function delCookie (name, options = {}) {
  const date = new Date()
  const path = options.path
  const domain = options.domain
  const secure = options.secure || ''
  date.setTime(date.getTime() - 10000)
  document.cookie = [name, '=', escape(''), date.toGMTString(), path, domain, secure].join('')
}
